

#include "organiser.h"
#include "ruleio.h"
#include "ruleinterpreter.h"
#include "audioinfo.h"
#include "fileoperations.h"
#include <map>
#include <typeinfo>

using namespace Org;


const char* TAGGED_MP3 = "/home/krone/Desktop/Organiser/New/exit012/one.mp3";
bf::path Organiser::incoming_dir("/");
void (*Organiser::cb_ptr_func)() = 0;



Organiser::Organiser()
{

}

void Organiser::init() const
{    
    Vars::map_action["copy"] = Vars::ORG_COPY;
    Vars::map_action["move"] = Vars::ORG_MOVE;
    Vars::map_action["delete"] = Vars::ORG_DELETE;
    Vars::map_action["copy_dir"] = Vars::ORG_COPY_DIR;

    Org::RuleIO rule_reader;
    Organiser::instance()->rules = rule_reader.read_xml(ORGANISER_RULES_PATH);
}


void Organiser::get_rule_groups(void* gui)
{
    Organiser::instance()->cb_object = gui;

    // URL_RULE_GROUPS
    HttpRequest::request((char*)"http://localhost:3000/rule_groups/",
                         Organiser::cb_parse_rulegroups_response);
}

// callback for http request
size_t Organiser::cb_parse_rulegroups_response(void *buffer, size_t size,
                                               size_t nmemb, void *userp)
{
    Organiser::instance()->rule_groups = Org::RuleIO::parse_rulegroup_xml((char*)buffer);
    Organiser::cb_ptr_func();
    cout << (char*)buffer << endl;
    return size*nmemb;
}

void Organiser::execute_matches()
{
    for(vector<MatchedPath>::const_iterator i = rule_matches.begin(); i!= rule_matches.end(); ++i)
    {
        MatchedPath match = (MatchedPath)*i;
        if(match.ok_to_run) perform_operation(&match);
    }
}

void Organiser::perform_operation(MatchedPath *data)
{
    Rule cur_rule = data->rule;
    bf::path cur_path = data->path;

    switch(cur_rule.action)
    {
        case(Vars::ORG_MOVE):
            FileOperations::move_dir(incoming_dir, cur_path, cur_rule.to_dir);
            break;
        case(Vars::ORG_COPY):
            FileOperations::copy_dir(incoming_dir, cur_path, cur_rule.to_dir);
            break;
        case(Vars::ORG_DELETE):
            FileOperations::del_dir(cur_path);
            break;
        case(Vars::ORG_RENAME):
            FileOperations::rename(cur_path, cur_rule.to_dir);
            break;

        default:
            cerr << "File operation not found in Vars" << endl;

    }
}




bool Org::Organiser::run_rules()
{
    bf::directory_iterator end_itr;
    
    for(bf::directory_iterator itr(Organiser::incoming_dir.string()); itr!=end_itr; ++itr)
    {
        bf::path cur_path = itr->path();

        for(vector<Rule>::const_iterator i = Org::Organiser::rules.begin(); i!=Org::Organiser::rules.end(); ++i)
        {
            Rule cur_rule = (Rule)*i;            
            
            if(RuleInterpreter::rule_complies(cur_rule, cur_path))
            {
                MatchedPath matched_path = MatchedPath();
                matched_path.path = cur_path;
                matched_path.rule = cur_rule;
                matched_path.ok_to_run = true;

                matched_path.to_path = FileOperations::get_new_path(incoming_dir, cur_path, cur_rule.to_dir);
                rule_matches.push_back(matched_path);
            }
        }
    }
    return true;
}

void Organiser::http_get(char* url, HttpRequest::callback ptr_f)
//void Organiser::http_get(char* url, size_t (*ptr_f)(void*, size_t,size_t, void*))
{
    HttpRequest::request(url, ptr_f);
}

void Organiser::http_get_discogs(char* search, HttpRequest::callback ptr_f)
{
    string url = Discogs::domain + search +Discogs::api_suffix;
    Organiser::http_get((char*)url.c_str(), ptr_f);
}


